﻿using System;
using System.IO;
using System.Text;

namespace SuiHan.Commons.Logging {
    public class LoggerConsole : TextWriter {
        private readonly TextWriter _out;
        public override Encoding Encoding => _out.Encoding;

        /// <summary>
        /// 是否启用控制台输出
        /// </summary>
        public static bool OutputEnabled { get; set; } = true;

        private static LoggerConsole _instance;

        private LoggerConsole(TextWriter writer) {
            _out = writer;
        }

        public static void Install() {
            if (_instance == null) {
                Console.OutputEncoding = Encoding.UTF8;
                _instance = new LoggerConsole(Console.Out);
                Console.SetOut(_instance);
            }
        }

        public override void Write(char value) {
            if (OutputEnabled) _out.Write(value);
        }
    }
}
